package com.ajie.service.impl;


import com.ajie.entity.SysMenu;
import com.ajie.entity.SysPermission;
import com.ajie.entity.SysRole;
import com.ajie.mapper.SysMenuMapper;
import com.ajie.mapper.SysPermissionMapper;
import com.ajie.mapper.SysRoleMapper;
import com.ajie.service.SysRoleService;
import com.ajie.utils.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
@Slf4j
public class SysRoleServiceImpl implements SysRoleService {


    @Autowired
    private SysRoleMapper roleMapper;
    
    @Autowired
    private SysMenuMapper menuMapper;
    
    @Autowired
    private SysPermissionMapper permissionMapper;
    
    @Autowired
    private RedisUtil redisUtil;


    @Override
    public Result findPage(@RequestBody QueryInfo queryInfo) {
        log.info("该文件名是：SysRoleServiceImpl,该方法是：findPage");
        log.info("1.开始角色数据分页-->页码{}, -->页数{}, -->查询内容{}",queryInfo.getPageNumber(),queryInfo.getPageSize(),queryInfo.getQueryString());
        PageHelper.startPage(queryInfo.getPageNumber(),queryInfo.getPageSize());
        Page<SysRole> page = roleMapper.findPage(queryInfo.getQueryString());
        long total = page.getTotal();
        log.info("2.查询的总条数是-->{}",total);
        List<SysRole> result = page.getResult();
        log.info("3.分页列表-->{}",result);
        result.forEach(item ->{
            //查询角色下的菜单信息
            List<SysMenu>  menus = roleMapper.findMenusByRoleId(item.getId());
            menus.forEach((menu) ->{
                List<SysMenu> childrenMenus = roleMapper.findChildrenMenusByRoleId(menu.getId(), item.getId());
                menu.setChildren(childrenMenus);
            });
            item.setMenus(menus);
            //查询角色下的权限信息
            List<SysPermission>  permissions = roleMapper.findPermissionsByRoleId(item.getId());
            item.setPermissions(permissions);
        });
        return new PageResult(total, result);
    }
    
    @Override
    public Result findAllRoles() {
        log.info("该文件名是：SysRoleServiceImpl,该方法是：findAllRoles");
        String queryInfo = null;
        List<SysRole> roles = roleMapper.findPage(queryInfo);
        return Result.success("查询所有角色成功",roles);
    }
    
    
    @Transactional
    @Override
    public Result insert(SysRole role) {
        log.info("该文件名是：SysRoleServiceImpl,该方法是：insert");
        //健壮性判断
        SysRole role1 = roleMapper.findByLabel(role.getLabel());
        if (null != role1){
            return Result.fail("添加失败，该角色已存在");
        }
        //插入角色信息
        roleMapper.insert(role);
        log.info("添加对应的权限数据");
        role.getPermissions().forEach(item ->roleMapper.insertPermissions(role.getId(),item.getId()));
        log.info("添加对应的菜单数据");
        role.getMenus().forEach(item ->roleMapper.insertMenus(role.getId(),item.getId()));
        redisUtil.delKey("userInfo_"+SecurityUtil.getUsername());
        return Result.success("添加角色数据成功");
    }

    @Override
    public Result delete(Long id) {
        log.info("该文件名是：SysRoleServiceImpl,该方法是：delete");
        //先将角色信息查出来
        log.info("查询该角色信息下是否有菜单和权限");
        if(roleMapper.findMenusByRoleId(id).size() > 0){
            roleMapper.deleteMenuByRoleId(id);
        }
        if (roleMapper.findPermissionsByRoleId(id).size() > 0){
            roleMapper.deletePermissionByRoleId(id);
        }
        //删除角色信息
        roleMapper.delete(id);
        redisUtil.delKey("userInfo_"+SecurityUtil.getUsername());
        return Result.success("删除角色数据成功");
    }
    
    @Transactional
    @Override
    public Result update(SysRole role) {
        log.info("该文件名是：SysRoleServiceImpl,该方法是：update");
        roleMapper.update(role);
        if(roleMapper.findPermissionsByRoleId(role.getId()).size()>0){
            log.info("先删除对应的权限数据");
            roleMapper.deletePermissionByRoleId(role.getId());
        }
        log.info("再添加对应的权限数据");
        role.getPermissions().forEach(item ->roleMapper.insertPermissions(role.getId(),item.getId()));
        
        if(roleMapper.findAllMenusByRoleId(role.getId())>0){
            log.info("先删除对应的菜单数据");
            roleMapper.deleteMenuByRoleId(role.getId());
        }
        role.getMenus().forEach(item ->roleMapper.insertMenus(role.getId(),item.getId()));
        redisUtil.delKey("userInfo_"+SecurityUtil.getUsername());
        return Result.success("修改角色数据成功");
    }

    
}
